#coding:utf-8

import sys
from subprocess import call
import os
import ConfigParser
import platform
#获取程序根目录
def get_home_dir():
    home_dir=""
    conf = ConfigParser.ConfigParser()
    conf.read("../config/gol.cfg")
    if platform.system()=="Windows" :
       home_dir = conf.get("gol", "home_dir")
    else:
       home_dir = conf.get("gol", "home_dir_linux")
    return home_dir
home_dir =  get_home_dir()

#跑model生成数据
def run_model(file_path):
    full_file_name = os.path.basename(file_path)
    id = full_file_name.split('.')[0]
    path = str(id)+'.txt'
    print path
    print '模型文件： '+file_path
    outputPath = home_dir+'result/output_04/'+path
    print outputPath
    call("/tmp/CRF/crf_test -m /tmp/CRF/abc/model "+file_path+" >> " + outputPath,shell=True)


# 遍历指定目录，显示目录下的所有文件名
def eachFile(filepath):
    pathDir = os.listdir(filepath)
    file_names = []
    for allDir in pathDir:
        child = os.path.join('%s%s' % (filepath, allDir))
        child.decode('gbk')  # .decode('gbk')是解决中文显示乱码问题
        file_names.append(child)
    print '原始文档目录扫描完成'
    return file_names


#循环处理数据
def run_datas():
    #循环读取文件夹内的数据
    dir_path = home_dir + 'result/product_03/'
    file_names = eachFile(dir_path)
    for file_name_path in file_names:
        run_model(file_name_path)
    print  '生产数据处理完成'


if __name__ == "__main__":
    run_datas()